package kos.robert.core;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Window;

import kos.robert.screens.ScreenManager;

public abstract class Core {

	private static final DisplayMode modes[] = { new DisplayMode(1024, 768, 32, 60), new DisplayMode(1024, 768, 24, 0),
			new DisplayMode(1024, 768, 16, 0), new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0),
			new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0),
			new DisplayMode(640, 480, 16, 0) };
	private boolean running;
	protected ScreenManager screen;

	// stop method
	public void stop() {
		running = false;
	}

	// call init and gameloop
	public void run() {
		try {
			init();
			gameLoop();
		} finally {
			screen.restoreScreen();
		}
	}

	// set to full screen
	public void init() {
		screen = new ScreenManager();
		DisplayMode dm = screen.findFirsCompatibleDisplaymode(modes);
		screen.setFullScreen(null);

		Window w = screen.getFullScreenWindow();
		w.setFont(new Font("Arial", Font.PLAIN, 20));
		w.setBackground(Color.BLACK);
		w.setForeground(Color.GREEN);
		running = true;
	}

	// main gameLoop
	public void gameLoop() {
		long startTime = System.currentTimeMillis();
		long cumTime = startTime;

		while (running) {
			long timePassed = System.currentTimeMillis() - cumTime;
			cumTime += timePassed;
			update(timePassed);

			Graphics2D g = screen.getGraphics();
			draw(g);
			g.dispose();
			screen.update();

			try {
//				Thread.yield();
			} catch (Exception e) {
			}
		}
	}

	// update animation
	public void update(long timePassed) {
	}

	// draws the screen
	public abstract void draw(Graphics2D g);
}
